import 'dart:math';

import 'package:hong_vpn_new_20240626/utils/utils.dart';
import 'package:shared_preferences/shared_preferences.dart';

import '../models/ai_model.dart';

class AiLikedChanged {}

class AiChatsChanged {}

class AiConfig {
  static List<AiModel> chats = [];

  static addChat(AiModel? model) {
    if (model == null) {
      return;
    }
    chats.removeWhere((element) => element.title == model.title);
    chats.insert(0, model);
    eventBus.fire(AiChatsChanged());
  }

  static List<AiModel> get normals => _normals();
  static List<AiModel> _normals() {
    return [
      AiModel(
          title: "Fox Fairy",
          header: "assets/images/Rectangle 49.png",
          likeNum: Random().nextInt(10000),
          description:
              "Alisa is a kind,thoughtful 20-year-oldwith long bronn hair and green eyes. Sheis pursuing an English degree and hopesto become a teacher. Alisa is quiet yetwrm with an nntimieti, snirit",
          labels: AiLabel.randoms()),
      AiModel(
          title: "Nuroty",
          header: "assets/images/Rectangle 50.png",
          likeNum: Random().nextInt(10000),
          description:
              "Though she may appear reserved at first, her warm and intimate spirit quickly shines through, drawing people in with ease. With a deep-seated love for language and literature, she diligently pursues her English degree, fueled by a heartfelt dream of becoming a teacher. Her innate ability to engage and inspire, coupled with her unwavering commitment to her studies, positions her perfectly for a fulfilling career in education.",
          labels: AiLabel.randoms()),
      AiModel(
          title: "Emial Heler",
          header: "assets/images/Rectangle 51.png",
          likeNum: Random().nextInt(10000),
          description:
              "I am Emial Heler, a passionate and curious individual. My name is unusual, but it is as unique and meaningful as I am. I love to explore the unknown, whether it is the ocean of knowledge or the nuances of life, I have great interest and enthusiasm.",
          labels: AiLabel.randoms()),
      AiModel(
          title: "Expeorer",
          header: "assets/images/Rectangle 52.png",
          likeNum: Random().nextInt(10000),
          description:
              "I have always maintained a desire for learning and an attitude of pursuing excellence. I firmly believe that knowledge is the key to endless possibilities, and continuous learning is the power to unlock new worlds. Therefore, I actively participate in the study of various disciplines, and constantly broaden my vision and cognitive boundaries.",
          labels: AiLabel.randoms()),
      AiModel(
          title: "Saikeler",
          header: "assets/images/Rectangle 53.png",
          likeNum: Random().nextInt(10000),
          description:
              "I like reading, from literary classics to popular science books, each book is like a wise man, leading me into a different world and experiencing a different life. At the same time, I am also keen on traveling and photography, to record every unforgettable moment with the lens, and to frame the beauty of the world as eternal.",
          labels: AiLabel.randoms()),
      AiModel(
          title: "Kisn Sofhd",
          header: "assets/images/Rectangle 54.png",
          likeNum: Random().nextInt(10000),
          description:
              "I am a person who loves life, pursues dreams and dares to explore. I look forward to meeting more like-minded friends in the coming days and writing our wonderful chapter together",
          labels: AiLabel.randoms()),
    ];
  }

  static Future<List<AiModel>> cacheModels() async {
    SharedPreferences prefs = await SharedPreferences.getInstance();
    var value = prefs.getStringList("AiConfig_AI_MODELS_CACHE");
    var list = value?.map((e) => AiModel.fromJson(e)).toList();
    if (list == null) {
      list = normals;
      await prefs.setStringList(
          "AiConfig_AI_MODELS_CACHE", normals.map((e) => e.toJson()).toList());
    }
    return list;
  }

  static reloadCacheAisBy(AiModel m) async {
    var models = await cacheModels();
    var newModels = models.map((e) => e.title == m.title ? m : e).toList();
    SharedPreferences prefs = await SharedPreferences.getInstance();
    await prefs.setStringList(
        "AiConfig_AI_MODELS_CACHE", newModels.map((e) => e.toJson()).toList());

    eventBus.fire(AiLikedChanged());
  }
}
